<template>
  <dialog-component
    :dialog-visible="state.show"
    size="410px"
    title="审核"
    :btn-text="state.approveType == '1' ? '审核通过' : '审核拒绝'"
    @close-dialog="handleClose()"
    @save-dialog="handleSubmit()"
  >
    <el-form ref="formRef" :model="state.formData" label-width="auto" class="my-form-box2">
      <el-form-item
        label="审核意见:"
        prop="approveComments"
        class="all-width"
        :rules="[
          { required: state.approveType == '1' ? false : true, message: '请输入审核意见', trigger: 'blur' }
        ]"
      >
        <el-input
          v-model="state.formData.approveComments"
          type="textarea"
          placeholder="请输入审核意见"
        ></el-input>
      </el-form-item>
    </el-form>
  </dialog-component>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { approveApi, rejectApi } from '../api'
import { useProxy } from '@/utils/proxy'
const emit = defineEmits(['handleAdded'])
const formRef = ref<FormInstance>()
const { proxy } = useProxy()
const state = reactive({
  isSubmiting: false,
  approveId: [],
  approveType: '1', // 1通过 2拒绝
  show: false,
  formData: {
    approveComments: ''
  },
  tableList: []
})
// 关闭回调
const handleClose = () => {
  state.show = false
  state.approveId = []
  state.approveType = '1'
  state.formData = {
    approveComments: ''
  }
}
// 提交
const handleSubmit = async () => {
  await formRef.value?.validate(async (valid) => {
    if (valid) {
      if (state.isSubmiting) return
      state.isSubmiting = true
      const params = { ...state.formData } as any
      if (state.approveType == '1') {
        params.idStr = state.approveId
        approveApi(params)
          .then(() => {
            proxy.$message.msgSuccess('审核通过')
            state.isSubmiting = false
            handleClose()
            emit('handleAdded')
          })
          .catch(() => {
            state.isSubmiting = false
          })
      } else {
        rejectApi(state.approveId[0], params)
          .then(() => {
            proxy.$message.msgSuccess('拒绝成功')
            state.isSubmiting = false
            handleClose()
            emit('handleAdded')
          })
          .catch(() => {
            state.isSubmiting = false
          })
      }
    }
  })
}
defineExpose({ state })
</script>

<style lang="scss" scoped>
.my-form-box2 {
  :deep(.el-textarea__inner) {
    min-height: 100px !important;
  }
}
</style>
